package com.moore.service.Impl;

import com.moore.bean.Area;
import com.moore.bean.City;
import com.moore.bean.Province;
import com.moore.dto.ResponseData;
import com.moore.mapper.AreaMapper;
import com.moore.service.AreaService;
import com.moore.service.EntitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.moore.utils.Logger.toLog;

@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    AreaMapper areaMapper;
    @Autowired
    EntitiesService entitiesService;

    @Override
    public ResponseData addArea(String province, String city, String area, String remarks) {
        toLog("查找省份"+province);
        List<Province> provinceList = areaMapper.getProvince(province); // 获取省份
        if (provinceList.isEmpty()) { // 如果没有该省份
            toLog("没有找到该省份，执行添加");
            int result = areaMapper.addProvince(province);// 添加省份
            if (result == 0) { // 添加省份失败
                toLog("添加省份失败");
                return new ResponseData(500,"添加省份失败",null,0);
            } else toLog("添加省份成功");
        } else toLog("找到该省份，不执行添加");
        toLog("查找城市"+city);
        List<City> cityList = areaMapper.getCity(province,city); // 获取城市
        if (cityList.isEmpty()) { // 如果没有该城市
            toLog("没有找到该城市，执行添加");
            int result = areaMapper.addCity(province, city);// 添加城市
            if (result == 0) { // 添加城市失败
                toLog("添加城市失败");
                return new ResponseData(500, "添加城市失败", null, 0);
            } else toLog("添加城市成功");
        } else toLog("找到该城市，不执行添加");
        toLog("查找区域"+area);
        List<Area> areaList = areaMapper.getArea(province, city, area,10,0); // 获取区域
        if (areaList.isEmpty()) { // 如果没有该区域
            toLog("没有找到该区域，执行添加");
            int result = areaMapper.addArea(province, city, area, remarks);// 添加区域
            if (result == 0) { // 添加区域失败
                toLog("添加区域失败");
                return new ResponseData(500, "添加区域失败", null, 0);
            } else toLog("添加区域成功");
        } else {
            toLog("找到该区域，不执行添加");
            return new ResponseData(500, "该区域已存在", null, 0);
        }
        toLog(province+city+area+"添加成功");
        return new ResponseData(200,"添加成功",null,1);
    }

    @Override
    public ResponseData getProvince() {
        List<Province> provinceList = areaMapper.getProvince("");
        toLog("获取省份成功");
        return new ResponseData(200,"获取成功",provinceList,provinceList.size());
    }

    @Override
    public ResponseData getCity(String province) {
        List<City> cityList = areaMapper.getCity(province,"");
        toLog("获取城市成功");
        return new ResponseData(200,"获取成功",cityList,cityList.size());
    }

    @Override
    public ResponseData queryArea(String province, String city, String area, int size, int curr) {
        if (size == 0){
            size = 10;
        }
        List<Area> areaList = areaMapper.getArea(province, city, area, size, curr);
        // 获取所有区域名称
        List<String> areaNames = areaList.stream()
                .map(Area::getName)
                .collect(Collectors.toList());
        // 批量查询所有区域的小区数量
        List<Map<String, Object>> communityCountsList = areaMapper.getCommunityCounts(areaNames);
        // 将结果集转换为 Map
        Map<String, Integer> communityCounts = communityCountsList.stream()
                .collect(Collectors.toMap(
                        map -> (String) map.get("name"),
                        map -> ((Long) map.get("communityCount")).intValue()
                ));
        // 设置小区数量
        areaList.forEach(value -> value.setCommunityCount(communityCounts.getOrDefault(value.getName(), 0)));
        toLog("获取区域成功: " + areaNames);
        return new ResponseData(200, "获取成功", areaList, areaMapper.getAreaCount(province, city, area));
    }


    @Override
    public ResponseData updateArea(Area area) {
        int result = areaMapper.updateArea(area);
        if (result == 1){
            toLog("修改成功");
            return new ResponseData(200,"修改成功",null,1);
        }else {
            toLog("修改失败");
            return new ResponseData(500,"修改失败",null,0);
        }
    }

    @Override
    public ResponseData deleteArea(List<Area> areas) {
        return entitiesService.deleteEntities(areas,"area_name","已删除");
    }
}
